#!/sbin/openrc-run
# Copyright 1999-2021 Gentoo Authors
# Copyright 2021 Maxim Karasev <begs@disroot.org>
# Distributed under the terms of the GNU General Public License v2

extra_commands="configtest"
extra_started_commands="reload"

name="gmid"
description="Simple and secure Gemini server"
description_configtest="Run gmid's internal config check."
description_reload="Reload the gmid configuration without losing connections."
supervisor=supervise-daemon
command=/usr/bin/gmid
command_args="-c \"$gmid_config_file\" -P \"$gmid_pid_file\" -f"

depend() {
	need net
	use dns logger netmount
}

start_pre() {
	if [ "${RC_CMD}" != "restart" ]; then
		configtest || return 1
	fi
}

stop_pre() {
	if [ "${RC_CMD}" = "restart" ]; then
		configtest || return 1
	fi
}

reload() {
	configtest || return 1
	ebegin "Refreshing gmid's configuration"
	start-stop-daemon --signal SIGHUP --pid "$gmid_pid_file"
	eend $? "Failed to reload gmid"
}

configtest() {
	ebegin "Checking gmid's configuration"
	${command} -c "${gmid_config_file}" -n
	eend $? "failed, please correct errors in the config file"
}
